<?php

namespace addons\shop\controller;

use addons\epay\library\Service;
use think\Db;
use think\Validate;

class Info extends Base
{
    protected $noNeedLogin = ['list'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
    public function tableList($param)
    {

        $pageNum = 15;
        if (isset($param['num']) && !empty($param['num'])) {
            $pageNum = $param['num'];
        }

        return self::where(function ($query) use ($param) {


        })->order('createtime desc')->paginate($pageNum);
    }
    public function list(){
        $param = $this->request->param();

        $type = $this->request->get('type',1);
        $sort_id = $this->request->get('sort_id','');
        $where['type'] = $type;
        if($sort_id){
            $where['sort_id'] = $sort_id;
            $param['sort_id'] = $sort_id;
        }
        $param['type'] = $type;
        $list = \addons\shop\model\Info::tableList($param);
        $user_id=$this->auth->id;
        foreach ($list as $k=>$v){
            if($user_id){
                $fabulous = Db::name("info_user_fabulous")->where(['user_id'=>$user_id,'info_article_id'=>$v['id']])->find();
                $collection = Db::name("info_user_collection")->where(['user_id'=>$user_id,'info_article_id'=>$v['id']])->find();
                if($fabulous){
                    $list[$k]['is_fabulous'] = 1;
                }else{
                    $list[$k]['is_fabulous'] = 0;
                }
                if($collection){
                    $list[$k]['is_collection'] = 1;
                }else{
                    $list[$k]['is_collection'] = 0;
                }
            }else{
                $list[$k]['is_fabulous'] = 0;
                $list[$k]['is_collection'] = 0;
            }
        }
        $info_sort = Db::name("info_sort")->select();

        $this->view->assign('data', $list);
        $this->view->assign('info_sort', $info_sort);
        $this->view->assign('type', $type);
        $this->view->assign('sort_id', $sort_id);
        $this->view->assign('t_type', 6);
        $this->view->assign('infoList', $list);
        return $this->view->fetch('/info/list');
    }
    public function detail(){
        $article_id = $this->request->get('article_id');
        $where['id'] = $article_id;
        $train = Db::name("info_article")->where($where)->find();
        $info_sort = Db::name("info_sort")->select();
        $com_data = Db::name("info_user_comment i")
            ->field("i.*,u.nickname")
            ->join("user u", 'u.id=i.user_id')
            ->where('i.info_article_id',$article_id)
            ->select();
        $this->view->assign('info_sort', $info_sort);
        $this->view->assign('data', $train);
        $this->view->assign('data_comment', $com_data);
        $this->view->assign('t_type', 6);
        return $this->view->fetch('/info/detail');
    }
    /**
     * @return void
     * 点赞数
     */
    public function fabulous(){
        $user_id=$this->auth->id;
        $article_id = $this->request->post('article_id');
        if(!$article_id) $this->error("缺少必传参数");
        $d = Db::name("info_user_fabulous")->where(['user_id'=>$user_id,'info_article_id'=>$article_id])->find();
        if(!$d) {
            $save1 = Db::name("info_article")->where("id",$article_id)->setInc("fabulous_sum",1);
            $add_data['user_id'] = $user_id;
            $add_data['info_article_id'] = $article_id;
            $add_data['createtime'] = time();
            $add_data['updatetime'] = time();
            $add1 = Db::name("info_user_fabulous")->insert($add_data);
            if($save1 && $add1){
                $this->success('成功');
            }else{
                $this->error("失败");
            }
        }else{
            $save1 = Db::name("info_article")->where("id",$article_id)->setDec("fabulous_sum",1);
            $add_data['user_id'] = $user_id;
            $add_data['info_article_id'] = $article_id;
            $add1 = Db::name("info_user_fabulous")->where($add_data)->delete();
            if($save1 && $add1){
                $this->success('成功');
            }else{
                $this->error("失败");
            }
        }

    }
    /**
     * @return void
     * 评论数
     */
    public function comment(){
        $user_id=$this->auth->id;
        $article_id = $this->request->post('article_id');
        $comment = $this->request->post('comment');
        if(!$article_id || !$comment) $this->error("缺少必传参数");
        $save1 = Db::name("info_article")->where("id",$article_id)->setInc("comment_sum",1);
        $add_data['user_id'] = $user_id;
        $add_data['info_article_id'] = $article_id;
        $add_data['comment'] = $comment;
        $add_data['createtime'] = time();
        $add_data['updatetime'] = time();
        $add1 = Db::name("info_user_comment")->insert($add_data);
        if($save1 && $add1){
            $this->success('成功');
        }else{
            $this->error("失败");
        }
    }
    /**
     * @return void
     * 收藏数
     */
    public function collection(){
        $user_id = $this->auth->id;
        $article_id = $this->request->post('article_id');
        if(!$article_id) $this->error("缺少必传参数");
        $d = Db::name("info_user_collection")->where(['user_id'=>$user_id,'info_article_id'=>$article_id])->find();
        if(!$d) {
            $save1 = Db::name("info_article")->where("id",$article_id)->setInc("collection_sum",1);
            $add_data['user_id'] = $user_id;
            $add_data['info_article_id'] = $article_id;
            $add_data['createtime'] = time();
            $add_data['updatetime'] = time();
            $add1 = Db::name("info_user_collection")->insert($add_data);
            if($save1 && $add1){
                $this->success('成功');
            }else{
                $this->error("失败");
            }
        }else{
            $save1 = Db::name("info_article")->where("id",$article_id)->setDec("collection_sum",1);
            $add_data['user_id'] = $user_id;
            $add_data['info_article_id'] = $article_id;

            $add1 = Db::name("info_user_collection")->where($add_data)->delete();
            if($save1 && $add1){
                $this->success('成功');
            }else{
                $this->error("失败");
            }
        }

        /*$save1 = Db::name("info_article")->where("id",$article_id)->setInc("collection_sum",1);
        $add_data['user_id'] = $user_id;
        $add_data['info_article_id'] = $article_id;
        $add_data['createtime'] = time();
        $add_data['updatetime'] = time();
        $add1 = Db::name("info_user_collection")->insert($add_data);
        if($save1 && $add1){
            $this->success('成功');
        }else{
            $this->error("失败");
        }*/
    }
    /**
     * @return void
     * 打赏数
     */
    public function reward(){
        $user_id=$this->auth->id;
        $article_id = $this->request->post('article_id');
        $reward_money = $this->request->post('reward_money');
        $type = $this->request->post('type','wechat');
        $method = $this->request->post('method','wap');
        if(!$article_id || !$reward_money) $this->error("缺少必传参数");
        /* $save1 = Db::name("info_article")->where("id",$article_id)->setInc("reward_sum",$reward_money);
         $add_data['user_id'] = $user_id;
         $add_data['info_article_id'] = $article_id;
         $add_data['money'] = $reward_money;
         $add_data['createtime'] = time();
         $add_data['updatetime'] = time();
         $add1 = Db::name("info_user_reward")->insert($add_data);
         if($save1 && $add1){
             $this->success('成功');
         }else{
             $this->error("失败");
         }*/
        //订单号
        $out_trade_no = date("YmdHis") . mt_rand(100000, 999999);
        //回调链接
        $notifyurl = $this->request->root(true) . '/addons/epay/index/inforeward/paytype/' . $type;
        $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;

        $info = get_addon_info('third');
        if (!$info || !$info['state']) {
            $this->error("请在后台安装第三方登录插件");
        }
        try {
            $response=Service::submitOrder($reward_money, $out_trade_no, $type, "订单支付", $notifyurl, $returnurl,$method);
            //$response = Service::submitOrder($params);
            if($response)
            {
                $add_data['user_id'] = $user_id;
                $add_data['info_article_id'] = $article_id;
                $add_data['money'] = $reward_money;
                $add_data['status'] = 2;
                $add_data['order'] = $out_trade_no;
                $add_data['paytype'] = $type;
                $add_data['createtime'] = time();
                $add_data['updatetime'] = time();
                $add1 = Db::name("info_user_reward")->insert($add_data);
                if(!$add1){
                    $this->error("数据有误");
                }
            }
        } catch (GatewayException $e) {
            throw new \Exception(config('app_debug') ? $e->getMessage() : "支付失败，请稍后重试");
        }
        //$response['order'] = $out_trade_no;
        //$this->success('成功',$response);
       return $response;
    }
    /**
     * @return void
     * 购买
     */
    public function buy(){
        $user_id=$this->auth->id;
        $article_id = $this->request->post('article_id');
        $type = $this->request->post('type','wechat');
        $method = $this->request->post('method','wap');
        if(!$article_id) $this->error("缺少必传参数");
        //订单号
        $out_trade_no = date("YmdHis") . mt_rand(100000, 999999);
        //回调链接
        $notifyurl = $this->request->root(true) . '/addons/epay/index/infobuy/paytype/' . $type;
        $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;

        $info = get_addon_info('third');
        if (!$info || !$info['state']) {
            $this->error("请在后台安装第三方登录插件");
        }
        $data = Db::name("info_article")->where('id',$article_id)->find();
        if(!$data) $this->error("数据有误");
        $money = $data['money'];
        try {
            $response=Service::submitOrder($money, $out_trade_no, $type, "订单支付", $notifyurl, $returnurl,$method);
            //$response = Service::submitOrder($params);
            if($response)
            {
                $add_data['user_id'] = $user_id;
                $add_data['info_article_id'] = $article_id;
                $add_data['money'] = $money;
                $add_data['status'] = 2;
                $add_data['order'] = $out_trade_no;
                $add_data['paytype'] = $type;
                $add_data['createtime'] = time();
                $add_data['updatetime'] = time();
                $add1 = Db::name("info_user_buy")->insert($add_data);
                if(!$add1){
                    $this->error("数据有误");
                }
            }
        } catch (GatewayException $e) {
            throw new \Exception(config('app_debug') ? $e->getMessage() : "支付失败，请稍后重试");
        }
        //$response['order'] = $out_trade_no;
        $this->success('成功',$response);
    }
    /**
     * @return void
     * 入会申请
     */
    public function societyAdd(){
        $user_id=$this->auth->id;
        $name = $this->request->post('name');
        $sex = $this->request->post('sex');
        $age = $this->request->post('age');
        $duties = $this->request->post('duties');
        $tel = $this->request->post('tel');
        $mail = $this->request->post('mail');
        $address = $this->request->post('address');
        $company = $this->request->post('company');
        if(!$name || !$sex || !$age || !$duties || !$tel || !$address || !$company) $this->error("缺少必传参数");
        if ($mail && !Validate::is($mail, "email")) {
            $this->error(__('Email is incorrect'));
        }
        if ($tel && !Validate::regex($tel, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $add_data['uid'] = $user_id;
        $add_data['name'] = $name;
        $add_data['sex'] = $sex;
        $add_data['age'] = $age;
        $add_data['duties'] = $duties;
        $add_data['tel'] = $tel;
        $add_data['mail'] = $mail;
        $add_data['address'] = $address;
        $add_data['company'] = $company;
        $add1 = Db::name("society")->insert($add_data);
        if($add1){
            $this->success('成功');
        }else{
            $this->error("失败");
        }
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 资讯分类
     */
    public function info_sort(){
        $re_data =  Db::name("info_sort")->select();
        $this->success('成功',$re_data);

    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 收藏
     */
    public function info_collection(){
        $user_id=$this->auth->id;
        $type = $this->request->post('type');
        if($type){
            $w['ar.type'] = $type;
        }
        $w['t.user_id'] = $user_id;
        $son_data = Db::name("info_user_collection t") ->join('info_article ar', 't.info_article_id=ar.id')->where($w)->field("ar.*")->select();
        $this->success('成功',$son_data);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info_comment(){
        $user_id=$this->auth->id;
        $type = $this->request->post('type');
        if($type){
            $w['ar.type'] = $type;
        }
        $w['t.user_id'] = $user_id;
        $son_data = Db::name("info_user_comment t") ->join('info_article ar', 't.info_article_id=ar.id')->where($w)->field("t.*,ar.title")->select();
        $this->success('成功',$son_data);
    }
}
